﻿using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wimm.Common.Entities.Utils;
using Wimm.Common.Interfaces.Services;

namespace Wimm.Common.Entities
{
    public class Expense: Transaction
    {
        public Expense()
        {
            //For SQL Light usage only
        }

        public Expense(DateTime time, int amount, int userId, int categoryId, String comment)
            :base(amount,time,userId)
        {
            this.ExpenseCategoryId = categoryId;
            this.Comment = comment;
        }

        public Expense(long time, int amount, int userId, int categoryId, String comment)
            : base(amount, time, userId)
        {
            this.ExpenseCategoryId = categoryId;
            this.Comment = comment;
        }

        public Decimal AmountInDollars
        {
            get { return (Decimal)this.Amount / 100.00M; }
        }

        public int ExpenseCategoryId { get; protected set; }

        public String Comment { get; set; }
    }

    public class ExpenseIcon : Expense
    {
        public ExpenseIcon(Expense expence, Icon icon, String categoryName, IExpenseService expenceService)
        {
            this.Id = expence.Id;
            this.Amount = expence.Amount;
            this.ExpenseCategoryId = expence.ExpenseCategoryId;
            this.Time = expence.Time;
            this.UserId = expence.UserId;
            this.Comment = expence.Comment;
            this.Icon = icon;
            this.CategoryName = categoryName;
            this.ExpenseService = expenceService;
            this.Command = new RelayCommand(SaveCommand);
        }

        private async void SaveCommand()
        {
            var expence = new Expense(this.Time, this.Amount, this.UserId, this.ExpenseCategoryId, this.Comment);
            expence.SetId(this.Id);
            await ExpenseService.EditExpense(expence);
            Messenger.Default.Send(new ExpenseUpdateGridItemMessenge(this.Id, this.Comment));
        }

        private IExpenseService ExpenseService;
        public RelayCommand Command { get; protected set; }
        public String CategoryName { get; protected set; }
        public Icon Icon { get; protected set; }
    }
}
